/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.audioeditor.soundmanagers;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ahmed
 */
public class FileConverter implements Converter {

    @Override
    public int convert(Object source, Object target, Object props) {

        int returnDta = 0;
        try {
            File fsource = (File) source;
            File ftarget = (File) target;
            
            Encoder encoder = new Encoder();
            
            System.out.println("decodelist="+Arrays.deepToString(encoder.getSupportedDecodingFormats()));
            MultimediaInfo info = encoder.getInfo(fsource);
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("sonicls");
            audio.setBitRate(info.getAudio().getBitRate());
            audio.setChannels(info.getAudio().getChannels());
            audio.setSamplingRate(info.getAudio().getSamplingRate());

            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("rm");
            attrs.setAudioAttributes(audio);

            System.out.println(info.getDuration());
            
            encoder.encode(fsource, ftarget, attrs);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
            returnDta = 1;
        } catch (InputFormatException ex) {
            Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
            returnDta = 2;
        } catch (EncoderException ex) {
            Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
            returnDta = 3;
        }
        return returnDta;
    }
}
